package com.aphrodite.ui.widgets;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.aphrodite.util.geom.GeomUtil;
import com.aphrodite.util.geom.Rectangle;

/**
 * An image that can contain points of interest.
 * <p>
 * These points are represented as rectangles
 * </p>
 * 
 * @author kentelt
 * 
 */
public class ImageMap extends TraversableWidget {

	private Vector areas = new Vector();
	private boolean canRenderPoints = true;
	private final Image image;
	private int index;
	private SpotRenderer renderer;

	/**
	 * 
	 * @param theme
	 * @param image
	 * @throws NullPointerException
	 *             if <code> image</code> is null
	 */
	public ImageMap(final Theme theme, Image image) {
		super(theme, 0, 0);
		if (image == null)
			throw new NullPointerException();
		this.image = image;
		updateModel();
	}

	/**
	 * 
	 * @param area
	 *            depicts the point of the area and
	 * @throws IllegalArgumentException
	 *             if the rectangle is not within the image dimensions.
	 */
	public void addLocation(Rectangle area) {
		if (GeomUtil.isInside(area, new Rectangle(0, 0, width, height))) {
			areas.addElement(area);
			reDrawWidget();
		} else
			throw new IllegalArgumentException("Rectangle not in image");
	}

	public void canRenderPoints(boolean renderPoints) {
		this.canRenderPoints = renderPoints;
	}

	private void decreaseIndex() {
		if (index > 0) {
			index--;
		}
		reDrawWidget();
	}

	public Image getImage() {
		return image;
	}

	private void increaseIndex() {
		if (index < areas.size() - 1) {
			index++;
		}
		reDrawWidget();
	}

	public boolean moveDown() {
		increaseIndex();
		return false;
	}

	public boolean moveUp() {
		decreaseIndex();
		return false;
	}

	public final void render(final Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		g.drawImage(image, 0, 0, 0);
		Rectangle rect;
		/* Render Points */
		if (canRenderPoints) {
			g.setColor(theme.fgColor);
			for (int i = 0; i < areas.size(); i++) {
				rect = (Rectangle) areas.elementAt(i);
				if (renderer != null)
					renderer.drawSpot(rect, g);
				else
					g.fillRect(rect.getX() - 1, rect.getY() - 1, 2, 2);
			}
		}
		if (areas.size() > 0) {
			rect = (Rectangle) areas.elementAt(index);
			if (renderer != null) {
				renderer.drawSelectedSpot(rect, g);
			} else {
				g.setColor(theme.highLightColor);
				g.drawRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
			}
		}
		release(g, width, height, true);
	}

	public void setSpotRenderer(final SpotRenderer renderer) {
		this.renderer = renderer;
	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		return false;
	}

	protected void updateModel() {
		width = image.getWidth();
		height = image.getHeight();
		reDraw();
	}

}
